'''
Created on 2016年2月22日

@author: Administrator
'''
from asyncore import dispatcher
import asyncore, socket

PORT = 5005

class ChatServer(dispatcher):
    def __init__(self, port):
        dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()#可以在服务器没有正确关闭的情况下， 重用同一个地址（端口）
        self.bind(('', port))
        self.listen(5)
        
    def handle_accept(self):
        conn, addr = self.accept()
        print('connection attempt from ', addr[0])
        print('connection attempt from port ', conn)
        
        
if __name__ == '__main__':
    s = ChatServer(PORT)
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        pass